package com.ara.project_common.ui.page;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableArrayList;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ara.base.ui.page.BaseActivity;
import com.ara.common.image.GlideUtils;
import com.ara.project_common.BR;
import com.ara.project_common.R;
import com.ara.project_common.data.api.CommonRouterApi;
import com.ara.project_common.databinding.ItemLargePictureBinding;
import com.blankj.utilcode.util.BarUtils;
import com.kunminx.architecture.ui.page.DataBindingConfig;

import java.util.List;

import pw.xiaohaozi.adapter_plus.adapter.SimpleAdapter;
import pw.xiaohaozi.adapter_plus.holder.ViewHolder;

/**
 * Created by XieXin on 2022/9/9.
 * 大图查看
 */
@Route(path = CommonRouterApi.API_LARGE_PICTURE_VIEW)
public class LargePictureViewActivity extends BaseActivity {
    private LargePictureViewModel mState;
    private LargePictureAdapter adapter;
    private final ObservableArrayList<String> list = new ObservableArrayList<>();

    @Autowired
    List<String> images;
    @Autowired
    int index;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(LargePictureViewModel.class);
        adapter = new LargePictureAdapter();
        adapter.refresh(list);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_large_picture_view, BR.vm, mState)
                .addBindingParam(BR.adapter, adapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarVisibility(this, false);
        ARouter.getInstance().inject(this);
        if (images == null || images.isEmpty()) finish();

        list.addAll(images);

        mState.count.setValue(images.size());
    }

    public class ClickProxy {

        public void back() {
            finish();
        }

    }

    public static class LargePictureAdapter extends SimpleAdapter<ItemLargePictureBinding, String> {

        @Override
        protected void onBindViewHolder(@NonNull ViewHolder<ItemLargePictureBinding> holder, int position,
                                        @NonNull ItemLargePictureBinding binding, @Nullable String data, int checkIndex) {
            GlideUtils.loadImage(mContext, data, binding.photo);

        }
    }

    public static class LargePictureViewModel extends ViewModel {
        public final MutableLiveData<Integer> count = new MutableLiveData<>(0);
        public final MutableLiveData<String> index = new MutableLiveData<>("0/0");
    }

    public static class LargePictureBindingAdapter {
        @BindingAdapter(value = {"largePictureIndex"}, requireAll = false)
        public static void largePictureIndex(ViewPager2 pager2, LargePictureViewModel viewModel) {
            pager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                @Override
                public void onPageSelected(int position) {
                    viewModel.index.setValue((position + 1) + "/" + viewModel.count.getValue());
                }
            });
        }
    }
}